/**
 * Created by liebling on 2017/7/1.
 */
"use strict";
import $script from 'scriptjs';
import '../main';
import '../../css/userCenter/userManage.less';
import '../../css/parts/common.less';

var ApiService = require('../module/ApiService');
var UserService = require('../module/UserService');

$script.ready('jqueryFile', function () {
	require('../component/common/header');
	require('../component/common/header2');
	require('../component/common/workNav');
	require('../component/common/list');
	require('../../../public/plugins/laydate/laydate');
	var index = layer.load(1, {shade: [1, '#fff'], scrollbar: false});
	avalon.filters.statusName = function (val) {
		switch (val) {
			case 'used':
				return '启用';
				break;
			case 'disable':
				return '已禁用';
				break;
			case 'delete':
				return '已删除';
				break;
			case 'locked':
				return '锁定';
				break;
			default:
		}
	};
	var paramsData = [
		{
			name: 'userList',
			params: $.extend({
				userName: '',
				status: '',
				registerTime: '',
				registerTimeEndDate: '',
				pageNumber: 1,
				pageSize: 10,
			}, ApiService.getParams())
		},
	];
	var pageData = avalon.define({
		$id: 'pageData',
		data: {
			mallList: {},
		},
		config: {
			mallList: {
				params: {
					userName: '',
					status: '',
					registerTime: '',
					registerTimeEndDate: '',
					pageNumber: 1,
					pageSize: 10,
				},
				filterParams: {},
				listName: 'data.results',
				post: 'userList'
			}
		},
		init: function () {
			$('.layui-input').on('click', function () {
				console.log(laydate)
				laydate({istoday: false});

			});
		},
		del: function (el) {
			var Index = layer.load(1, {shade: [0.3, '#000'], scrollbar: false});
			ApiService.postApi('deleteUser', {id: el._id})
				.then(ApiService.error)
				.then(function (res) {
					layer.msg(res.message, {
						time: 2000
					}, function () {
						$('.list-view').trigger('refresh');
						layer.close(Index);

					})
				})
				.catch(function (err) {
					layer.close(Index);
				});
		},
		//批量修改
		changeStatus: function (items, status) {
			var Index = layer.load(1, {shade: [0.3, '#000'], scrollbar: false});
			var params = {
				status: status,
				ids: _.compact(_.map(items, function (item) {
					if(item.status != status ) {
						return item._id
					}else {
						return 0;
					}
				}))
			};
			if(_.size(params.ids)){
				params.ids = params.ids.join(',');
				ApiService.postApi('update', params)
					.then(ApiService.error)
					.then(function (res) {
						layer.msg(res.message, {
							time: 2000
						}, function () {
							$('.list-view').trigger('refresh');
							layer.close(Index);

						})
					})
					.catch(function (err) {
						layer.close(Index);
					});
			}else {
				layer.msg('选择数据已经是当前状态，请重新选择。',{},function () {
					layer.close(Index);
				})
			}
			
		}
	});
	ApiService.addTaskList(paramsData)
		.then(function () {
			return ApiService.doTaskList();
		})
		.then(function (data) {
			pageData.data.mallList = data.userList;
			$('.list-view').trigger('reload');
			pageData.init();
			layer.close(index);
			return null;
		})
		.catch(function (error) {
		});
});
